﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media.Imaging;
using WickedFlame.Core;
using WickedFlame.Core.Shapes;
using WickedFlame.Util.Data;

namespace WickedFlame.Util.Actions
{
    public class PasteAction
    {
        List<VectorShape> _shapes;
        DrawingCanvas _canvas;

        BitmapImage _image;
        public BitmapImage Image
        {
            get
            {
                return _image;
            }
        }

        public void DoAction(DrawingCanvas canvas)
        {
            _canvas = canvas;

            if (!canvas.SelectedLayer.IsSelecteable || !canvas.SelectedLayer.IsVisible)
                throw new Exception("Layer is not marked as selecteabel or layer is not visible.");

            IDataObject data = Clipboard.GetDataObject();

            if (data == null)
                return;

            var si = data.GetData(typeof(SerializableImage)) as SerializableImage;
            if (si != null)
            {
                _image = si.Source.Image;;
            }

            var sc = data.GetData(typeof(SerializableShapeContainer)) as SerializableShapeContainer;
            if (sc != null)
            {
                _shapes = new List<VectorShape>();
                VectorShape shape = sc.PullNext();

                while (shape != null)
                {
                    _shapes.Add(shape);
                    shape = sc.PullNext();
                }
            }

            if (si == null)
            {
                var src = data.GetData(typeof(BitmapSource)) as BitmapSource;
                
                var conv = new ConvertImageToPbgra32Action();
                _image = conv.ConvertBitmapFormat(src);
            }
        }

        public void FinalizeAction()
        {
            if (_image == null)
                throw new Exception("Image not generated! DoAction(DrawingCanvas) has to be called first.");

            if (_shapes != null)
            {
                Debug.WriteLine("Shapes pasted into canvas");

                foreach(VectorShape shape in _shapes)
                    _canvas.AddShape(shape);
            }
            else
            {
                Debug.WriteLine("Image pasted into canvas");

                var shape = new ImageShape(_image);
                _canvas.AddShape(shape, true);
                shape.Invalidate();
            }
        }





        //public static BitmapImage GetEmfFromClipboard()
        //{
        //    BitmapImage pi2 = null;
        //    IntPtr zero;
        //    //if (currentWindow == null)
        //    //{
        //    zero = IntPtr.Zero;
        //    //}
        //    //else
        //    //{
        //    //    zero = currentWindow.Handle;
        //    //}
        //    //if (NativeMethods.OpenClipboard(zero))
        //    if (OpenClipboard(zero))
        //    {
        //        try
        //        {
        //            //if (NativeMethods.IsClipboardFormatAvailable(14))
        //            bool test = IsClipboardFormatAvailable(17);
        //            //CF_DIBV5
        //            //if (IsClipboardFormatAvailable(14))
        //            if (IsClipboardFormatAvailable(17))
        //            {
        //                //IntPtr clipboardData = NativeMethods.GetClipboardData(14);
        //                IntPtr clipboardData = GetClipboardData(17);
        //                if (clipboardData != IntPtr.Zero)
        //                {
        //                    try
        //                    {
        //                        BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
        //                            clipboardData, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

        //                        //Metafile metafile = new Metafile(clipboardData, true);
        //                        //pi2 = metafile;

        //                        ImageConvertAction imconv = new ImageConvertAction();
        //                        pi2 = imconv.ConvertBitmapFormat(bitmapSource);

        //                    }
        //                    catch (Exception)
        //                    {
        //                    }
        //                }
        //            }
        //        }
        //        finally
        //        {
        //            //NativeMethods.CloseClipboard();
        //            CloseClipboard();
        //        }
        //    }
        //    //GC.KeepAlive(currentWindow);
        //    return pi2;
        //}


        //[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
        //[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        //public static extern bool OpenClipboard(IntPtr hWndNewOwner);

        //[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
        //[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        //public static extern bool IsClipboardFormatAvailable(uint format);

        //[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        //public static extern IntPtr GetClipboardData(uint format);

        //[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
        //[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        //public static extern bool CloseClipboard();
    }
}
